# -*- coding: utf-8 -*-
'''
Created on 2013-6-1
玩家
@author: 不懂d叛逆
'''
import ccjhContainer, ccjhMan, ccjhObj, ccjhSkill
class ccjhUser(ccjhContainer.ccjhContainer):
	def __init__(self):
		super(ccjhUser, self).__init__()
		self.data = {}
		self.skills = {}
		self.magics = {}
		self.hp = 0
		self.mp = 0
		self.eff_hp = 0
		self.max_mp = 0
		self.max_hp = 0
		self.enforce = 0
		self.team = -1
		return

	def setTo(self, obj):
		super(ccjhUser, self).setTo(obj)
		Type = obj.getType()
		if Type == 23:
			temp = obj.get("hp")
			if len(temp) > 0:
				self.hp = int(temp)
			temp = obj.get("mp")
			if len(temp) > 0:
				self.mp = int(temp)
			temp = obj.get("eh")
			if temp.isdigit():
				self.eff_hp = int(temp)
			temp = obj.get("mm")
			if temp.isdigit():
				self.max_mp = int(temp)
			temp = obj.get("jl")
			if temp.isdigit():
				self.enforce = int(temp)
			else:
				self.enforce = 0
			temp = self.get("tea")
			if len(temp) > 0:
				self.team = int(temp)
			temp = self.get("ap")
			if len(temp) > 0 and self.team == -1:
				self.team = 0
		elif Type == 15:
			temp = obj.get("e")
			if len(temp) > 0:
				self.hp = int(temp)
			temp = obj.get("f")
			if len(temp) > 0:
				self.mp = int(temp)
			temp = obj.get("i")
			if temp.isdigit():
				self.eff_hp = int(temp)
			temp = obj.get("h")
			if temp.isdigit():
				self.max_mp = int(temp)
			temp = obj.get("g")
			if temp.isdigit():
				self.max_hp = int(temp)
		return

	def debugShowItem(self):
		for (k, v) in self.items.items():
			print("物品", k, v.ToString(False))
		return

	def FlushEquip(self, obj):
		if isinstance(obj, ccjhObj.ccjhObj):
			begin = obj.get("B")
			I = "I" + begin
			E = "E" + begin
			i = int(begin)
			try:
				ID = int(obj.get(I))
			except:
				return
			while ID > 0:
				item = self.Find(ID)
				if isinstance(item, ccjhMan.ccjhMan):
					try:
						item.wieldPos = int(obj.get(E))
						if item.wieldPos > 0:
							item.data["w"] = 1  # canWield标记
					except:
						pass
				i += 1
				I = "I%d" % (i,)
				E = "E%d" % (i,)
				try:
					ID = int(obj.get(I))
				except:
					break
		return

	def FlushSkill(self, skill):
		Type = skill.getType()
		if Type == 13:
			if not isinstance(skill, ccjhSkill.ccjhSkill):return
			if skill.get("B") == "1":
				self.skills.clear()
			sname = skill.get("S")
			if sname in self.skills:
				self.skills[sname].setTo(skill)
			else:
				self.skills[sname] = skill
			if skill.get("E") == "1":
				for v in self.skills.values():
					highSkill = v.get("M")
					if len(highSkill) > 0:
						tSkill = self.FindSkill(highSkill)
						if isinstance(tSkill, ccjhSkill.ccjhSkill):
							tSkill.enabled = True
							tSkill.baseSkill = v.get("S")
		elif Type == 16:
			sname = skill.get("S")
			if sname in self.skills:
				self.skills[sname].setTo16(skill)
		return

	def FindSkill(self, skillName):
		if skillName in self.skills:
			return self.skills[skillName]
		return 0

	def FlushMagics(self, magic):
		skill = self.FindSkill(magic.get("S"))
		if isinstance(skill, ccjhSkill.ccjhSkill):
			newName = "%s之%s" % (skill.get("N"), magic.get("N"))
			magic.data["N"] = newName
			M = magic.get("M")
			magic.SetSkill(skill.get("S"))
			self.magics[magic.get("S") + M] = magic
		return

	def ToString(self, bID = True):
		result = ""
		if bID:
			title = self.get("t")
			Name = self.get("p")
			ID = self.get("o")
			if len(title) > 0:
				result = "%s %s(%s)" % (title, Name, ID)
			else:
				if len(ID) > 0:
					result = "%s(%s)" % (Name, ID)
				else:
					result = Name
			dis = self.get("dis")
			if len(dis) > 0:
				result += "<%s>" % (dis,)
			elif self.get("exe") == "1":
				result += "<打坐中>"
			elif self.get("net") == "1":
				result += "<断线中>"
			elif self.get("poi") == "1":
				result += "<毒发中>"
			elif self.get("net") == "1":
				result += "<发呆中>"
		else:
			result = self.get("N")
		return result

	def CombatString(self):
		name = self.get("p")
		if self.team != -1:
			dis = self.get("dis")
			if len(dis) > 0:
				name += "<%s>" % (dis,)
			elif self.get("exe") == "1":
				name += "<打坐中>"
			elif self.get("net") == "1":
				name += "<断线中>"
			elif self.get("poi") == "1":
				name += "<毒发中>"
			return "%-14s%-7s%-5s%d" % (name, self.get("sta"), self.get("ap"),self.mp)
		else:
			return name
